// pages/purchasedetails/purchasedetails.js
import * as api from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    productInfo: null,
    productId: 0,
    latitude: null,
    longitude: null,
    businessList: null,
    businessId: 0
  },
  bindPickerChange: function (e) {
    const index = e.detail.value
    const businessId = this.data.businessList[index]['id']
    this.setData({
      index: e.detail.value,
      businessId
    })
  },
  onSubmit() {
    const that = this
    const businessId = that.data.businessId
    const productId = that.data.productId
    if (!businessId) {
      wx.showToast({
        title: '请选择门店',
        icon: 'error'
      })
      return false
    }
    if (!productId) {
      wx.showToast({
        title: '商品ID必传',
        icon: 'error'
      })
      return false
    }
    api.customerCreateProductOrder(businessId, productId, 1)
      .then(res => {
        console.log(res)
        const order_sn = res.data.order_sn
        api.pay(order_sn)
          .then(res => {
            const paymentData = res.data
            wx.requestPayment(paymentData)
              .then(res => {
                wx.navigateTo({
                  url: '/pages/order/productOrder/index',
                })
              }).catch(err => console.log(err))
          }).catch(err => {
            wx.showToast({
              title: err.msg,
              icon: 'error'
            })
          })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const productId = options.id
    this.productDetail(productId)
    this.getBusinessList()
  },
  productDetail(productId) {
    const that = this
    api.productDetail(productId)
      .then(res => {
        const productInfo = res.data
        that.setData({
          productInfo,
          productId
        })
      }).catch(err => console.log(err))
  },
  getBusinessList(lat = null, lng = null) {
    const that = this
    const cityId = wx.getStorageSync('cityId')
    const latitude = lat ? lat : that.data.latitude
    const longitude = lng ? lng : that.data.longitude
    if (!latitude || !longitude) {
      that.getFuzzyLocation('businessList', that.getBusinessList)
      return false
    }
    api.businessList(cityId, latitude, longitude)
      .then(res => {
        const businessList = res.data
        const businessId = businessList[0]['id']
        that.setData({
          businessList,
          index: 0,
          businessId
        })
      }).catch(err => console.log(err))
  },
  getFuzzyLocation(call_function = '', callback = null) {
    const that = this
    wx.getFuzzyLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        that.setData({
          latitude,
          longitude
        })
        if (callback) {
          callback(latitude, longitude)
        } 
      },
      fail(err) {
        console.log(err)
        wx.showModal({
          title: '提示',
          content: '您未同意提供模糊地理位置信息，我们无法为您展示最近的门店信息',
          cancelText: '拒绝授权',
          confirmText: '去授权',
          complete: (res) => {
            if (res.cancel) {
              wx.showToast({
                title: '您拒绝了授权',
                icon: 'error'
              })
            }
            if (res.confirm) {
              wx.openSetting({
                success(res) {
                  if (res.authSetting["scope.userFuzzyLocation"]) {
                    that.getFuzzyLocation('getFuzzyLocation|openSetting')
                  }
                }
              })
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})